function DatabaseTests(name) 
{	
	this.base = TestCase;
	this.base(name);
	this.className = "DatabaseTests";
}
DatabaseTests.prototype = new TestCase;

var date1, date2, siteId1, siteId2, duration1, duration2, 
	testResource, testProperty,	testLiteral, anonResource;

DatabaseTests.testing = function () { return [ "file:///home/dafyddj/src/takecontrol/trunk/components/TakeControlService.js" ]; }

DatabaseTests.prototype.setUp = function()
{
	this.database = new Database();
	date1 = new Date(2006, 2, 28).getW3CDate();
	date2 = new Date(2006, 3, 20).getW3CDate();
	duration1 = 33;
	duration2 = 666;
	testResource = Database.rdf.GetResource("TEST_RESOURCE");
	testProperty = Database.rdf.GetResource("TEST_PROP");
	testLiteral10 = Database.rdf.GetIntLiteral(10);
	testLiteral50 = Database.rdf.GetIntLiteral(50);
	anonResource = Database.rdf.GetAnonymousResource();
	siteId1 = "www.google.com";
	siteId2 = "www.bbc.co.uk";
}

DatabaseTests.prototype.testCreateStoreWithInMemDatasource = function() {
	assertNotNull(this.database);
	assertTrue(this.database.dsAvailable());
}

DatabaseTests.prototype.testGetDateResource_NewDate = function () {
	var resource = this.database._getDateResource(date1);
	var dateLit = this.database._ds.GetTarget(resource, Database.dateStringProp, true);
	assertTrue(resource instanceof CI.nsIRDFResource);
	assertTrue(this.database.datesList.contains(resource));
	assertTrue(dateLit instanceof CI.nsIRDFLiteral);
	assertEquals(date1, dateLit.Value);
}

DatabaseTests.prototype.testGetDateResource_DateExists = function () {
	var resource = this.database._getDateResource(date1);
	var resource2 = this.database._getDateResource(date1);
	assertTrue(resource === resource2);
}

DatabaseTests.prototype.testCreateSessionEntry = function() 
{
	var duration = 60;	
	var sessionEntry = this.database._createSessionEntry(date1, duration);

	assertTrue("sessionEntry", sessionEntry instanceof CI.nsIRDFResource);

	var retDate = this.database._ds.GetTarget(sessionEntry, Database.dateProp, true);
	assertTrue("retDate", retDate instanceof CI.nsIRDFResource);
	var dateString = this.database._ds.GetTarget(retDate, Database.dateStringProp, true);
	assertTrue("dateString", dateString instanceof CI.nsIRDFLiteral);
	assertEquals(date1, dateString.Value);

	var retDur = this.database._ds.GetTarget(sessionEntry, Database.durationProp, true);
	assertTrue(retDur instanceof CI.nsIRDFInt);
	assertEquals(duration, retDur.Value);
}

DatabaseTests.prototype.testCreateSiteEntry = function() {	
	var site = this.database._createSiteEntry(siteId1);

	assertTrue(site.resource !== undefined);
	assertTrue(site.bag !== undefined);

	assertTrue(this.database._siteExists(siteId1));
	assertTrue(Database.rdfContainerUtils.IsBag(this.database._ds, site.resource));

	var siteName = this.database._ds.GetTarget(site.resource, Database.hostProp, true);
	assertTrue(siteName instanceof CI.nsIRDFLiteral);
	assertEquals(siteId1, siteName.Value);
}

DatabaseTests.prototype.testUpdateValueExists = function () {
	this.database._ds.Assert(testResource, testProperty, testLiteral10, true);

	Database.dsUpdate(this.database._ds, testResource, testProperty, testLiteral50);

	var retObject = this.database._ds.GetTarget(testResource, testProperty, true);
	assertEquals(testLiteral50, retObject);
	assertFalse("original value shouldn't exist in datasource", this.database._ds.hasArcIn(testLiteral10, testProperty));
}

/* DatabaseTests.prototype.testUpdateOrCreateSite_NewSite = function () {
	fail("Test not written");
}

DatabaseTests.prototype.testUpdateOrCreateSiteSession_SiteExists = function () {
	fail("Test not written");
}
 */
DatabaseTests.prototype.testSave = function () {
	this.database.save();
}

DatabaseTests.prototype.testGetDailyTotal_DayExists = function () {
	this.database.updateOrCreateDailyTotal(date1, duration1);
	var dailyTotal = this.database.getDailyTotal(date1);
	assertEquals(duration1, dailyTotal);
}

DatabaseTests.prototype.testGetDailyTotal_DayNonExistant = function () {
	var dailyTotal = this.database.getDailyTotal(date1);
	assertEquals(0, dailyTotal);	
}
